#!/bin/bash


TOKEN="TOKEN" 
CHANNEL_NAME="CHANNEL_NAME"

API_URL="https://api.telegram.org/bot$TOKEN/"


bash yemekhanejson.sh > data.json

current_month="$(date +%m)"

while true
do
	
	if [ "$(date +%m)" != $current_month ]
	then
		bash yemekhanejson.sh > data.json
	fi


	current_date=$(date +%Y-%m-%d)


	ogle=$(cat data.json | perl -ne '/'"$current_date"'","ogle":\{"corba":"(.*?)","ana":"(.*?)","vegan":"(.*?)","yardimci":"(.*?)","aperatif":"(.*?)"/ && print "$1\n$2\n$3\n$4\n$5";')

	
	aksam=$(cat data.json | perl -ne '/'"$current_date"'","ogle":\{"corba":".*?","ana":".*?","vegan":".*?","yardimci":".*?","aperatif":".*?"\},"aksam":\{"corba":"(.*?)","ana":"(.*?)","vegan":"(.*?)","yardimci":"(.*?)","aperatif":"(.*?)"/ && print "$1\n$2\n$3\n$4\n$5";')



	ogle_text="$(date +%d.+%m.+%Y)%0AÖğle Yemeği:%0A$ogle"
	curl -s -X POST $API_URL"sendMessage" -d chat_id=$CHANNEL_NAME -d text="$ogle_text" > /dev/null



	next_time=$(date -d 'today 16:00' +%s)
	current_time=$(date +%s)
	sleep $(($next_time - $current_time))



	aksam_text="$(date +%d.+%m.+%Y)%0AAkşam Yemeği:%0A$aksam"
	curl -s -X POST $API_URL"sendMessage" -d chat_id=$CHANNEL_NAME -d text="$aksam_text" > /dev/null


	next_time=$(date -d 'tomorrow 09:00' +%s)
	current_time=$(date +%s)
	sleep $(($next_time - $current_time))

done
